package org.ltsh.generate.config;

import com.alibaba.fastjson.JSONObject;
import org.ltsh.generate.bean.BeanUtils;
import org.ltsh.generate.templete.DataUtil;
import org.ltsh.generate.xml.ReadXml;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by fengjianbo on 2017/12/27.
 */
public class ReadConfig {
    public static void main(String[] args) {
        try {
            readConfig("/templete/autoConfig.xml");
            System.out.println(JSONObject.toJSONString(GlobalConfig.jdbcConfig));
            System.out.println(JSONObject.toJSONString(GlobalConfig.templeteConfigList));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static void readConfig(String path) throws Exception {
//        InputStream inputStream = ReadConfig.class.getClassLoader().getResourceAsStream(path);
        FileInputStream inputStream = new FileInputStream(path);
//        InputStream resourceAsStream = ReadConfig.class.getResourceAsStream(path);
        Map configuration = ReadXml.xmlToMap(inputStream, "configuration");
        inputStream.close();
        Map applicationConfigMap = (Map)configuration.get("applicationConfig");
        if(applicationConfigMap != null) {
//            ApplicationConfig applicationConfig = BeanUtils.mapToBean(applicationConfigMap, ApplicationConfig.class);
            GlobalConfig.applicationConfig = new ApplicationConfig();
            GlobalConfig.applicationConfig.putAll(applicationConfigMap);
        }
        Map jdbcConfigMap = (Map)configuration.get("jdbcConfig");
        if(jdbcConfigMap == null || jdbcConfigMap.isEmpty()){
            throw new Exception("jdbc配置错误");
        } else {
            JdbcConfig jdbcConfig = BeanUtils.mapToBean(jdbcConfigMap, JdbcConfig.class);
            GlobalConfig.jdbcConfig = jdbcConfig;
        }


        List templeteList = getList(configuration, "templeteList", "templete");

        List<TempleteConfig> templeteConfigList = new ArrayList<TempleteConfig>();
        for (Object obj : templeteList) {
            Map map = (Map) obj;
            TempleteConfig templeteConfig = BeanUtils.mapToBean(map, TempleteConfig.class);
            if(templeteConfig.getTempleteType() == null) {
                templeteConfig.setTempleteType("java");
            }
//            templeteConfig.setTargetPackage(GlobalConfig.applicationConfig.getBasePackage() + templeteConfig.getTargetPackage());
            templeteConfigList.add(templeteConfig);
            if(map.get("templete") != null) {
                templeteConfig.getExtraMap().putAll(GlobalConfig.applicationConfig);
                List extra = getList(map, "templete", "extras");
                if(extra != null) {
                    for (Object obj1 : extra) {
                        if(obj1 == null) {
                            continue;
                        }
                        Map map1 = null;
                        if(((Map) obj1).get("property") != null) {
                            map1 = (Map)((Map) obj1).get("property");
                        } else {
                            map1 = ((Map) obj1);
                        }
                        if(((String)map1.get("name")).endsWith("Package")) {
                            templeteConfig.getExtraMap().put((String)map1.get("name"), GlobalConfig.applicationConfig.getBasePackage() + map1.get("value"));
                        } else {
                            templeteConfig.getExtraMap().put((String)map1.get("name"), map1.get("value"));
                        }


                    }
                }
            }
        }
        GlobalConfig.templeteConfigList = templeteConfigList;
        List generateList = getList(configuration, "generateList", "generate");

//        List generateList = (List)configuration.get("generateList");
        List<GenerateConfig> generateConfigList = new ArrayList<GenerateConfig>();
        if(generateList != null) {
            for (Object obj : generateList) {
                Map map = (Map) obj;
                GenerateConfig generateConfig = BeanUtils.mapToBean(map, GenerateConfig.class);
                String entityName = DataUtil.getMethodName(generateConfig.getTableName());
                generateConfig.setClassName(entityName);
                generateConfig.setLowerClassName(DataUtil.getPropertyName(generateConfig.getTableName()));
                generateConfigList.add(generateConfig);
            }
        }

        GlobalConfig.generateConfigList = generateConfigList;
        List generateBatchList = getList(configuration, "generateBatchList", "generateBatch");
        if(generateBatchList != null) {
            for (Object obj : generateBatchList) {
                if(obj == null) {
                    continue;
                }
                Map map = (Map) obj;

                GenerateBatchConfig generateBatchConfig = BeanUtils.mapToBean(map, GenerateBatchConfig.class);
                String baseTempletePath = generateBatchConfig.getBaseTempletePath();
                File file = new File(GlobalConfig.applicationConfig.getProjectPath() + baseTempletePath);
                if(file.exists()) {
                    String[] list = file.list();
                    for (String fileName : list) {
                        GenerateConfig generateConfig = new GenerateConfig();

//                <!--<generate tableName="answer_topic_detail" fileName="answerTopicDetail.sql" refTemplete="sqlTemplete"></generate>-->
                        generateConfig.setTableName(generateBatchConfig.getTableName());

                        String modelName = fileName.substring(0, fileName.lastIndexOf("."));
                        String entityName = DataUtil.getMethodName(generateConfig.getTableName());
                        if(generateBatchConfig.getRemovePrefix() != null) {
                            if(generateConfig.getTableName().startsWith(generateBatchConfig.getRemovePrefix())) {
                                entityName = DataUtil.getMethodName(generateConfig.getTableName().replace(generateBatchConfig.getRemovePrefix(), ""));
                            }
                        }
                        generateConfig.setClassName(entityName);
                        generateConfig.setLowerClassName(DataUtil.getPropertyName(entityName));

                        if(modelName.equals("entity")) {
                            entityName = entityName + ".java";
                        }else if(modelName.equals("sql")) {

                            entityName = DataUtil.getPropertyName(entityName) + ".sql";
                        } else {
                            entityName = entityName + DataUtil.getMethodName(modelName) + ".java";
                        }
                        generateConfig.setModulesName(generateBatchConfig.getModulesName());
                        generateConfig.setRefTemplete(modelName + "Templete");
                        generateConfig.setFileName(entityName);
                        generateConfigList.add(generateConfig);
                    }
//                System.out.println(JSONObject.toJSONString(list));

                }
            }
        }

        System.out.println("配置文件解析完毕");
    }

    private static List getList(Map map, String parentKey, String key){
        List list = new ArrayList();
        if(map.get(parentKey) != null && map.get(parentKey) instanceof Map) {
            Object o = ((Map) map.get(parentKey)).get(key);
            if(o instanceof List) {
                list.addAll((List)((Map) map.get(parentKey)).get(key));
            } else {
                list.add((Map)((Map) map.get(parentKey)).get(key));
            }

        } else {
            list = (List)map.get(parentKey);
        }
        return list;
    }
}
